<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" placeholder="请选择" prop="manager">
        <el-select
          v-model="form.manager"
          placeholder="请选择"
          style="width:80%"
        >
          <el-option
            v-for="item in employees"
            :key="item.id"
            :value="item.username"
            :label="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" style="width:80%" placeholder="1-300个字符" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="onSubmit">确定</el-button>
        <el-button size="small" @click="quxiao">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
// 获取员工简单数据列表
import { getEmployeeSimple } from '@/api/employees'
// 增删改查
import { addDepartments, editDepartments, getDepartDetail } from '@/api/departments'
export default {
  name: '',
  components: {},
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    ruleList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义效验规则
    const ruleCode = (rule, value, callback) => {
      const { ruleList, id, isEdit } = this
      // 如果是编辑
      if (isEdit) {
        ruleList.filter(item => item.id !== id).map(item => item.code).includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
      }
      // 如果是添加
      ruleList.map(item => item.code).includes(value) ? callback(new Error('编码' + value + '已经存在')) : callback()
    }
    const ruleName = (rule, value, callback) => {
      const { ruleList, id, isEdit } = this
      // 如果是编辑
      if (isEdit) {
        const isPid = ruleList.find(item => item.id === id).pid
        ruleList.filter(item => item.pid === isPid && item.id !== id).map(item => item.name).includes(value) ? callback(new Error('名称' + value + '已经存在')) : callback()
      }
      // 如果是添加
      ruleList.filter(item => item.pid === id).map(item => item.name).includes(value) ? callback(new Error('名称' + value + '已经存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [],
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: ruleName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { validator: ruleCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getSimple()
    this.getXiangQing()
  },
  mounted() {},
  methods: {
    //   取消按钮
    quxiao() {
      this.$emit('quxiao')
    },
    //   确定按钮
    onSubmit() {
      // 兜底校验
      this.$refs.form.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    // 获得员工简单列表
    async  getSimple() {
      const res = await getEmployeeSimple()
      // console.log('员工信息', res.data)
      this.employees = res.data
    },
    // 添加
    async doAdd() {
      try {
        const res = await addDepartments({ ...this.form, pid: this.id })
        // console.log(res)
        if (res.success) {
          this.$emit('success')
          this.$message({ type: 'success', message: '添加成功' })
        }
      } catch (err) {
        console.log('添加失败', err)
        this.$message({ type: 'error', message: err })
      }
    },
    // 编辑
    async doEdit() {
      try {
        const res = await editDepartments({ ...this.form, id: this.id })
        if (res.success) {
          this.$emit('success')
          this.$message({ type: 'success', message: '修改成功' })
        }
      } catch (err) {
        console.log('修改失败', err)
        this.$message({ type: 'error', message: err })
      }
    },
    // 获取部门详情
    async getXiangQing() {
      if (this.isEdit) {
        const res = await getDepartDetail(this.id)
        // console.log('部门详情', res)
        this.form = res.data
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
